<template>
    <div>
        <el-container>
            <el-header class="project-header">
                <el-row>
                    <el-col>
                        <h2>zingon-doc内部文档系统</h2>
                    </el-col>
                </el-row>
            </el-header>
            <el-main>
                <div class="setting-row">
                    <el-row :gutter="40">
                        <el-card shadow="always" class="setting-card">
                            <div slot="header" class="clearfix">
                                <router-link to="/">
                                    <span>返回</span>
                                </router-link>
                            </div>
                            <el-tabs v-model="activeName" tab-position="right">
                                <el-tab-pane label="基础信息" name="first">
                                    <el-form :model="project">
                                        <el-form-item label="项目名称">
                                            <el-input v-model="project.name" auto-complete="off"></el-input>
                                        </el-form-item>
                                        <el-form-item label="项目描述">
                                            <el-input v-model="project.description" auto-complete="off"></el-input>
                                        </el-form-item>
                                        <el-button type="primary" @click="saveProject">确 定</el-button>
                                    </el-form>
                                </el-tab-pane>
                                <el-tab-pane label="成员管理" name="second">
                                    <el-button size="mini" @click="dialogFormVisible = true">添加成员</el-button>
                                    <el-table :data="tableData" style="width: 100%" max-height="450">
                                        <el-table-column prop="name" label="姓名">
                                        </el-table-column>
                                        <el-table-column prop="addDate" label="添加日期" width="180">
                                        </el-table-column>
                                        <el-table-column prop="power" label="权限">
                                        </el-table-column>
                                        <el-table-column fixed="right" label="操作" width="100">
                                            <template slot-scope="scope">
                                                <el-button @click="deleteUser(scope.row)" type="text" size="small">删除</el-button>
                                            </template>
                                        </el-table-column>
                                    </el-table>
                                </el-tab-pane>
                                <el-tab-pane label="高级设置" name="third" class="settting-deletebtn">
                                    <el-button type="danger" round @click="deleteProject">删除</el-button>
                                </el-tab-pane>
                            </el-tabs>
                        </el-card>
                    </el-row>
                </div>
            </el-main>
        </el-container>
        <el-dialog title="添加成员" :visible.sync="dialogFormVisible" width="30%">
            <el-form>
                <el-form-item>
                    <el-input v-model="name" placeholder="请输入成员名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-radio-group v-model="power">
                        <el-radio label="cru">编辑</el-radio>
                        <el-radio label="r">只读</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveUserProject">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
  addProject,
  getProjectById,
  listUserByProjectId,
  addUserProject,
  delUserProject,
  delProject
} from "../../api";
import { Notification } from "element-ui";
export default {
  name: "mdedit",
  props: ["id"],
  data: function() {
    return {
      activeName: "first",
      project: {},
      tableData: [],
      dialogFormVisible: false,
      name: "",
      power: "cru"
    };
  },
  methods: {
    deleteProject() {
      let _this = this;
      delProject(_this.id)
        .then(function(resp) {
          console.log(resp);
          Notification.success("删除成功");
          _this.$router.push("/");
        })
        .catch(function(e) {
          if (e === "0x07002") {
            Notification.warning("抱歉，你没有权限。");
          }
        });
    },
    deleteUser(row) {
      console.log(row);
      let _this = this;
      delUserProject(row.id)
        .then(function(resp) {
          if (resp.errmsg === "ok" && resp.data === 1) {
            let index = _this.tableData.indexOf(row);
            if (index > -1) {
              _this.tableData.splice(index, 1);
              Notification.success("删除成功");
            }
          }
        })
        .catch(function(e) {
          if (e === "0x07001") {
            Notification.warning("不能删除自己。");
          } else if (e === "0x07002") {
            Notification.warning("抱歉，你没有权限。");
          }
        });
    },
    saveProject() {
      let _this = this;
      addProject(this.project).then(function(resp) {
        Notification.success("保存成功");
      });
    },
    saveUserProject() {
      let _this = this;
      addUserProject(_this.project.id, _this.name, _this.power)
        .then(function(resp) {
          _this.tableData.push(resp.data);
          Notification.success(_this.name + " 添加成功。");
          _this.dialogFormVisible = false;
        })
        .catch(function(e) {
          if (e === "0x05001") {
            Notification.error(_this.name + " 用户不存在。");
          } else if (e === "0x05002") {
            Notification.error(_this.name + " 用户已绑定此项目。");
          } else if (e === "0x06001") {
            Notification.error(_this.name + " 权限不对。");
          } else if (e === "0x07002") {
            Notification.error("抱歉，你没有权限。");
          }
        });
    }
  },
  mounted: function() {
    let _this = this;
    this.$nextTick(function() {
      getProjectById(_this.id).then(function(resp) {
        _this.project = resp.data;
      });
      listUserByProjectId(_this.id).then(function(resp) {
        _this.tableData = resp.data;
      });
    });
  }
};
</script>

<style scoped>
.project-header {
  align-items: center;
  display: flex;
  font-size: 30px;
}
.setting-row {
  display: flex;
  align-items: center;
  flex-direction: column;
}

.setting-row .el-row {
  width: 35%;
}

.setting-card {
  height: 550px;
}

.settting-deletebtn {
  display: flex;
  align-items: center;
  flex-direction: column;
  margin-top: 180px;
}
</style>
